package com.ecr.hub.front.validator;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;

/**
 * @author Patrice
 * @date 11 juil. 2011
 */
public abstract class AbstractGenericValidator extends AbstractValidator {

	public final void validate(FacesContext arg0, UIComponent arg1, Object arg2)
			throws ValidatorException {

		FacesMessage msg = null;

		try {
			if (!executeValidator(arg0, arg1, arg2)) {
				msg = getMessageValidator(getMessageKey(), getAttributsList(),
						getSeverityLevel(), arg0, arg1);
			}
		} catch (Exception e) {
			msg = getMessageErrorInValidator(e);
		}
		if (null != msg)
			throw new ValidatorException(msg);
	}

	// Méthode qui vérifie si la condition est réalisée (oui/non).
	protected abstract boolean executeValidator(FacesContext arg0,
			UIComponent arg1, Object arg2) throws Exception;
}
